*Create map of countries by their share of "important" constitutions provisions. 
// global shapefiles "$main/1. Dataset Creation/Input/"

ssc install schemepack, replace

*Create Shapefile
spshape2dta "GIS_shapefiles/World_Countries_Generalized",  saving(world) replace

*Initial cleaning to prepare for fuzzy match
use world, clear
keep _ID _CX COUNTRY ISO COUNTRYAFF
sort _ID


*Fuzzy Match with World Bank coordinates data
matchit _ID COUNTRY using "Data_mapping_country_merged.dta", idu(id) txtu(country) override threshold(0.2)
gsort _ID -similscore
bysort _ID: gen val = _n

*Generate keep variable and futher cleaning. 
gen keep = .
replace keep = 1 if val == 1 & similscore == 1
sort _ID
replace country=strtrim(country)


replace keep = 1 if country == "Russia" & COUNTRY == "Russian Federation"
replace keep = 1 if country == "Democratic Republic of the Congo" & COUNTRY == "Congo DRC"
replace keep = 1 if country == "Turkey" & COUNTRY == "Turkiye"
replace keep = 1 if country == "Yemen Arab Republic" & COUNTRY == "Yemen"
replace keep = 1 if country == "Slovak Republic" & COUNTRY == "Slovakia"
replace keep = 1 if country == "Cote d'Ivoire" & COUNTRY == "Côte d'Ivoire"
replace keep = 1 if country == "Cape Verde" & COUNTRY == "Cabo Verde"
replace keep = 1 if country == "Brunei" & COUNTRY == "Brunei Darussalam"
replace keep = 1 if country == "Kyrgyz Republic" & COUNTRY == "Kyrgyzstan"


// Countries in our data that did not have their coordinates listed in the world bank data: Abkhazia, Solomon Islands, South Ossetia, Taiwan
gsort id -similscore
bysort id: gen already_good = 1 if keep == 1
sort id keep
by id: replace already_good = already_good[_n-1] if already_good == .
drop if already_good == 1 & keep != 1
keep if keep == 1
sort country
tempfile country_coords
save `country_coords', replace

*Merge the World Bank/CCP crosswalk with the World Bank Coordinates
use "Data_CoreProvisions_240423.dta", clear
ren origin country
// ren _merge _mergeprev
merge m:1 country using `country_coords'

keep if year == 2020

*dropping countries for which we did not find the coordinates in the world bank data (Abkhazia, Solomon Islands, South Ossetia, Taiwan)
br if _merge != 3
drop if _merge != 3
duplicates drop _ID, force
**********************************************************************
**********************************************************************


*Create map
set scheme white_tableau
spmap total_present using world_shp, id(_ID) fcolor(Reds) cln(5)  legend(pos(6) ring(6) size(1.5) rows(1) title("Number of Provisions")) legstyle(2) ///
ocolor(gs2 ..) osize(0.06 ..)
  
  
graph export "Figure_10.pdf", replace

erase world_shp.dta
erase world.dta









